package com.yanqu.road.server.cmd.activity.feast;

import com.yanqu.road.entity.activity.feast.data.FeastUserData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ShanHeFeastProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.feast.ShanHeFeastModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@CmdApi(reqMsg = ShanHeFeastProto.FeastPveBagRewardGetReq.class, respMsg = ShanHeFeastProto.FeastPveBagRewardGetResp.class)
@Cmd(code = Protocol.S_SHAN_HE_FEAST_PVE_GET_BAG_REWARD, desc = "pve背包打理")
public class FeastPveBagRewardGetCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ShanHeFeastModule module = player.getModule(ShanHeFeastModule.class);
        FeastUserData userData = module.getUserData();
        if(userData == null){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(userData.getTempReward().isNothing()){
            ret = GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
            return;
        }
        String reward = PropertyHelper.parsePropertyToString(userData.getTempReward());
        userData.setTempReward(new Property());
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.FeastActivity, eLogMoneyType.FeastActivityBagRewardGet);
        ShanHeFeastProto.FeastPveBagRewardGetResp.Builder clientMsg = ShanHeFeastProto.FeastPveBagRewardGetResp.newBuilder();
        clientMsg.setRet(0);
        clientMsg.setReward(reward);
        player.sendPacket(Protocol.U_SHAN_HE_FEAST_PVE_GET_BAG_REWARD, clientMsg);
        module.syncUserData();
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SHAN_HE_FEAST_PVE_GET_BAG_REWARD, ShanHeFeastProto.FeastPveBagRewardGetResp.newBuilder().setRet(ret));
        }
    }
}
